package com.itboye.ihomebank.controller;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;

import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DebugLog;
import com.itboye.ihomebank.util.SPUtils;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack.ICallBack;

/**
 * 登录相关控制类--状态模式</br></br>
 * 
 * 登录成功后调用setLoginState(new LoginedState());</br></br> 退出登录后调用setLoginState(new
 * UnLoginState());</br></br> 进入主Activity前一定要设置一次，要么登录要么未登录</br></br>
 * 
 * @author Young
 * 
 */
public class LoginController implements Observer {

	static UserPresenter userPresenter;
	private static ILoginState loginState = new UnLoginState();
	private static String registrationId;
	private static PushAgent mPushAgent;

	public static void setLoginState(ILoginState loginState) {

		LoginController.loginState = loginState;
		if (loginState instanceof UnLoginState) {
			DebugLog.v("bytag", "未登录。。。");
			// PushAgent mPushAgent = PushAgent.getInstance(MyApplcation.ctx);
			// mPushAgent.disable(new IUmengCallback() {
			//
			// @Override
			// public void onSuccess() {
			// // TODO Auto-generated method stub
			//
			// }
			//
			// @Override
			// public void onFailure(String arg0, String arg1) {
			// // TODO Auto-generated method stub
			//
			// }
			// });
		} else if (loginState instanceof LoginedState) {

			mPushAgent = PushAgent.getInstance(MyApplcation.ctx);
			// mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
			mPushAgent.onAppStart();
			String uid = (String) SPUtils.get(MyApplcation.ctx, null,
					SPContants.USER_ID, "");
//			mPushAgent.removeAlias(uid, "ihome", new ICallBack() {
//
//				@Override
//				public void onMessage(boolean arg0, String arg1) {
//					// TODO Auto-generated method stub
//					System.out.println(arg0 + "arg0" + arg1);
//				}
//			});
			mPushAgent.addExclusiveAlias(uid, "ihome", new ICallBack() {

				@Override
				public void onMessage(boolean arg0, String arg1) {
					// TODO Auto-generated method stub
					System.out.println(arg0 + "arg222" + arg1);
				}
			});
			// 开启推送并设置注册的回调处理
			// if (!mPushAgent.isEnabled()) {
			// mPushAgent.enable(new IUmengRegisterCallback() {
			//
			// @Override
			// public void onRegistered(String registrationId) {
			// // onRegistered方法的参数registrationId即是device_token
			//
			// setExclusiveAlias(registrationId);
			// }
			// });
			// }

			// String device_token = UmengRegistrar
			// .getRegistrationId(MyApplcation.ctx);
			// System.out.println("device_token"+device_token);
			// if (!TextUtils.isEmpty(device_token)) {
			// setExclusiveAlias(device_token);
			// }

		}
	}

	private static void bindKeJiXia() {
		// TODO Auto-generated method stub
		//
		// RequestQueue requestQueue = Volley.newRequestQueue(MyApplcation.ctx);
		// Map<String, String> map = new HashMap<String, String>();
		// map.put("name1", "value1");
		// map.put("name2", "value2");
		// JSONObject jsonObject = new JSONObject(map);
		// JsonRequest<JSONObject> jsonRequest = new
		// JsonObjectRequest(Method.POST,NetPublicConstant.URL, jsonObject,
		// new Response.Listener<JSONObject>() {
		// @Override
		// public void onResponse(JSONObject response) {
		// Log.d(TAG, "response -> " + response.toString());
		// }
		// }, new Response.ErrorListener() {
		// @Override
		// public void onErrorResponse(VolleyError error) {
		// Log.e(TAG, error.getMessage(), error);
		// }
		// })
		// {
		// //注意此处override的getParams()方法,在此处设置post需要提交的参数根本不起作用
		// //必须象上面那样,构成JSONObject当做实参传入JsonObjectRequest对象里
		// //所以这个方法在此处是不需要的
		// // @Override
		// // protected Map<String, String> getParams() {
		// // Map<String, String> map = new HashMap<String, String>();
		// // map.put("name1", "value1");
		// // map.put("name2", "value2");
		//
		// // return params;
		// // }
		//
		// @Override
		// public Map<String, String> getHeaders() {
		// HashMap<String, String> headers = new HashMap<String, String>();
		// // 公共参数
		// headers.put(NetPublicParams.APP_VERSION, "1.0.0");
		// headers.put(NetPublicParams.APP_TYPE, "android");
		// // headers.put(NetPublicParams.TIME, time);
		// headers.put(NetPublicParams.ALG, NetPublicParams.ALG_VALUE);
		// // headers.put(NetPublicParams.NOTIFY_ID, time);
		// headers.put("client_id", NetPublicParams.CLIENT_ID);
		// // this.url = url + "?client_id=" + NetPublicParams.CLIENT_ID;
		//
		//
		//
		// // headers.put("Accept", "application/json");
		// // headers.put("Content-Type", "application/json; charset=UTF-8");
		//
		// return headers;
		// }
		// };
		// requestQueue.add(jsonRequest);
	}

	private static void setExclusiveAlias(String registrationId) {
		// String uid = (String) SPUtils.get(MyApplcation.ctx, null,
		// SPContants.USER_ID, "");
		// PushAgent mPushAgent = PushAgent.getInstance(MyApplcation.ctx);
		// mPushAgent.setExclusiveAlias(uid, "ihome");
		// ByAlert.alert("token:" + registrationId + " alian:" + uid + "  type:"
		// + "ihome");
		// System.out.println("token:" + registrationId + " alian:" + uid
		// + "  type:" + "ihome");
		// DebugLog.v("bytag", "setExclusiveAlias   token:", registrationId,
		// " alian:", uid, "  type:", "sunsun");

		// //登录后绑定设备
		// MyJsonRequest<String> request = new
		// MyJsonRequest.Builder<String>().apiVer("100")
		// .typeKey("BY_Device_bind")
		// .param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
		// ""))
		// .param("device_token",registrationId)
		// .param("version",android.os.Build.VERSION.RELEASE)
		// .param("device_type","Android")
		// .requestListener(new XRequestListener<String>() {
		//
		// @Override
		// public void onResponse(String response) {
		//
		// }
		// }).errorListener(new XErrorListener() {
		//
		// @Override
		// public void onErrorResponse(Exception exception, int code, String
		// msg) {
		// if (exception instanceof CodeErrorException) {
		// Toast.makeText(App.ctx, msg, 0).show();
		// } else {
		// Toast.makeText(App.ctx, exception.toString(), 0).show();
		// }
		//
		// }
		// }).build();
		// HttpRequest.getDefaultRequestQueue().add(request);
		//

	}

	public static void onMyMoneyClick(Activity act, Object data) {
		loginState.onMyMoneyClick(act, data);
	}

	public static void onMyAppointmentClick(Activity act, Object data) {
		loginState.onMyAppointmentClick(act, data);
	}

	public static void onMyCollectionClick(Activity act, Object data) {
		loginState.onMyCollectionClick(act, data);
	}

	public static void onMySignClick(Activity act, Object data) {
		loginState.onMySignClick(act, data);
	}

	public static void onMyPersonalCenter(Activity act, Object data) {
		loginState.onMyPersonalCenter(act, data);
	}
	public static void beifen(Activity act, Object data) {
		loginState.beifen(act, data);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub

	}
}
